package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.enums.AppointmentEnum;
import cn.wolfcode.enums.ConsumptionEnum;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.*;
import cn.wolfcode.util.RequestPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IAppointmentService appointmentService;


    @RequestPermission(name="消费单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }

    @RequestPermission(name="结算",expression="consumption:account")
    @RequestMapping("/account")
    public String account(Long consumptionId, HttpSession session){
        //消费单结算
        if (consumptionId != null){
            Consumption consumption = consumptionService.get(consumptionId);
            consumption.setStatus(ConsumptionEnum.AUDIT.getValue());
            Employee employee = (Employee)session.getAttribute("USER_IN_SESSION");
            consumption.setPayee(employee);
            consumption.setPayTime(new Date());
            consumptionService.updateAccount(consumption);
            //更新预约单状态
            Long appointmentId = appointmentService.selectAppointmentIdByConsumptionId(consumptionId);
            appointmentService.updateStatus(appointmentId, AppointmentEnum.FINISH.getValue());

        }
        return "redirect:/consumption/list";
    }


    @RequestPermission(name="消费单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }


    @RequestPermission(name="消费单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption,Long statusId) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            if (statusId!=null){
                consumption.setStatus(ConsumptionEnum.AUDIT.getValue());
            }
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @RequestPermission(name="消费单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption",consumptionService.get(id));
            model.addAttribute("consumptionItems",consumptionItemService.queryByCno(consumption.getCno()));
            model.addAttribute("businesses",businessService.listAll());
            model.addAttribute("systemDictionaryItem1",systemDictionaryItemService.selectByType(1));
            model.addAttribute("systemDictionaryItem2",systemDictionaryItemService.selectByType(4));
        }
        return "consumption/input";
    }

    @RequestMapping("/save")
    public String saveFromAppointment(Long appointmentId,HttpSession session) {
        //修改预约单状态 , 生成结算单
        Long consumptionId = consumptionService.saveFromAppointment(appointmentId,session);

        return "redirect:/consumption/input?id="+consumptionId;
    }
}
